package com.pur.zsms.pmsnmanage.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.CipherUtil;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.dao.IRoleDao;
import com.pur.zsms.pmsnmanage.role.po.RolePo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.LdAddressPo;
import com.pur.zsms.pmsnmanage.user.po.UserLevelPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.LdAddressVo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.po.SupplierShopUserPo;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.supplier.dao.SupplierDao;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.po.SupplierTypePo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.systemsetting.datadictionary.po.Code;

@Service("userService")
public class UserServiceImpl implements IUserService {

	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "userDao")
	private IUserDao userDaoImpl;

	@Resource(name = "roleDao")
	private IRoleDao roleDaoImpl;

	@Resource(name = "supplierDao")
	private SupplierDao supplierDao;

	public SupplierDao getSupplierDao() {
		return supplierDao;
	}

	public void setSupplierDao(SupplierDao supplierDao) {
		this.supplierDao = supplierDao;
	}

	public IUserDao getUserDaoImpl() {
		return userDaoImpl;
	}

	public void setUserDaoImpl(IUserDao userDaoImpl) {
		this.userDaoImpl = userDaoImpl;
	}

	public PaginationSupport listUser(PageController pageController, UserVo userVo) throws GenericException {
		return userDaoImpl.listUser(pageController, userVo);
	}

	public PaginationSupport listUserForSupplierShop(PageController pageController, UserVo userVo) throws GenericException {
		return userDaoImpl.listUserForSupplierShop(pageController, userVo);
	}

	public boolean addUser(UserVo c, UserPo lg) throws GenericException {
		// 校验相同登录名 和电话
		if (checkUserPo(c)) {
			msg = "系统中有相同的账号或者相同的电话！";
			return false;
		}
		UserPo po = new UserPo();
		try {
			BeanUtils.copyProperties(po, c);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("输入参数有误!");
		}
		// MD5对用户密码加密
		CipherUtil cipher = new CipherUtil();
		String md5Pwd = cipher.generatePassword(po.getPassword());
		po.setPassword(md5Pwd);
		po.setInsertTime(PubFun.getCurrentDate());
		userDaoImpl.addUser(po);
		String[] roles = c.getRoles();
		if (roles != null) {
			for (String roleId : roles) {
				UserRolePo userRolePo = new UserRolePo();
				userRolePo.setUserId(po.getUserId());
				userRolePo.setRoleId(Long.parseLong(roleId.trim()));
				userRolePo.setInsertTime(new Date());
				userDaoImpl.addUserRole(userRolePo);
			}
		}
		// // 职位等级关联变设置
		// 查询公司董事长
		long companyUser = userDaoImpl.getCompanyUser(lg).get(0).getCompanyUser();
		UserLevelPo userLevelPo = new UserLevelPo();
		userLevelPo.setUserId(po.getUserId());
		userLevelPo.setLevelId(c.getLevelId());
		userLevelPo.setHigherUser(c.getHigherUser());
		userLevelPo.setProvince(c.getProvince());
		userLevelPo.setCity(c.getCity());
		userLevelPo.setCountry(c.getCountry());
		userLevelPo.setCompanyUser(companyUser);
		userDaoImpl.addUserLevel(userLevelPo);
		return true;
	}

	public boolean checkUserPo(UserVo c) throws GenericException {
		List<UserPo> list = userDaoImpl.getUserPoListByUserNameOrUserPhone(c.getUserName(), c.getUserPhone());
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	public void addSupplierShopUserPo(SupplierShopUserPo c) throws GenericException {
		supplierDao.addSupplierShopUserPo(c);
	}

	public long addUserForSup(UserVo c) throws GenericException {
		// 校验相同登录名 和电话

		if (checkUserPo(c)) {
			msg = "系统中有相同的账号或者相同的电话！";
			return 0;
		}

		UserPo po = new UserPo();
		try {
			BeanUtils.copyProperties(po, c);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("输入参数有误!");
		}
		// MD5对用户密码加密
		CipherUtil cipher = new CipherUtil();
		String md5Pwd = cipher.generatePassword(po.getPassword());
		po.setPassword(md5Pwd);
		po.setInsertTime(PubFun.getCurrentDate());
		userDaoImpl.addUser(po);
		String[] roles = c.getRoles();
		if (roles != null) {
			for (String roleId : roles) {
				UserRolePo userRolePo = new UserRolePo();
				userRolePo.setUserId(po.getUserId());
				userRolePo.setRoleId(Long.parseLong(roleId.trim()));
				userRolePo.setInsertTime(new Date());
				userDaoImpl.addUserRole(userRolePo);
			}
		}
		// // 职位等级关联变设置
		// 查询公司董事长 董事长为供货商id
		// long companyUser =
		// userDaoImpl.getCompanyUser(lg).get(0).getCompanyUser();
		UserLevelPo userLevelPo = new UserLevelPo();
		userLevelPo.setUserId(po.getUserId());
		userLevelPo.setLevelId(c.getLevelId());
		userLevelPo.setHigherUser(c.getHigherUser());
		userLevelPo.setProvince(c.getProvince());
		userLevelPo.setCity(c.getCity());
		userLevelPo.setCountry(c.getCountry());
		userLevelPo.setCompanyUser(c.getCompanyUser());
		userDaoImpl.addUserLevel(userLevelPo);
		return po.getUserId();
	}

	public long addUserForSupplier(SupplierVo c, UserPo lg) throws GenericException {
		UserPo po = new UserPo();
		try {
			BeanUtils.copyProperties(po, c);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("输入参数有误!");
		}
		// MD5对用户密码加密
		CipherUtil cipher = new CipherUtil();
		String md5Pwd = cipher.generatePassword(po.getPassword());
		po.setPassword(md5Pwd);
		po.setInsertTime(PubFun.getCurrentDate());
		userDaoImpl.addUser(po);
		String[] roles = c.getRoles();
		if (roles != null) {
			for (String roleId : roles) {
				UserRolePo userRolePo = new UserRolePo();
				userRolePo.setUserId(po.getUserId());
				userRolePo.setRoleId(Long.parseLong(roleId.trim()));
				userRolePo.setInsertTime(new Date());
				userDaoImpl.addUserRole(userRolePo);
			}
		}

		// // 职位等级关联变设置
		// 查询公司董事长
		UserLevelPo userLevelPo = new UserLevelPo();
		userLevelPo.setUserId(po.getUserId());
		userLevelPo.setLevelId(1);
		userLevelPo.setHigherUser(0);
		userLevelPo.setProvince(c.getProvince());
		userLevelPo.setCity(c.getCity());
		userLevelPo.setCountry(c.getCountry());
		userLevelPo.setCompanyUser(c.getCompanyId());
		userDaoImpl.addUserLevel(userLevelPo);

		return po.getUserId();
	}

	public void updateUser(UserVo c) throws GenericException {
		UserPo po = new UserPo();
		try {
			BeanUtils.copyProperties(po, c);
			// MD5对用户密码加密
			CipherUtil cipher = new CipherUtil();
			String md5Pwd = cipher.generatePassword(po.getPassword());
			po.setPassword(md5Pwd);

			// 增加供货商关联
			/*
			 * if (c.getSupId() != 0) { SupplierPo supplierPo =
			 * supplierDao.getSupPoBySupId(String.valueOf(c.getSupId()));
			 * supplierPo.setUserId(Integer.valueOf(c.getUserId()));
			 * supplierDao.updateSupplier(supplierPo); }
			 */
			// 增加权限表的更改
			List<UserLevelPo> userLevelPoList = userDaoImpl.getUserLevelPoByUserId(Long.valueOf(c.getUserId()));
			UserLevelPo userLevelPo = new UserLevelPo();
			if (userLevelPoList != null && userLevelPoList.size() > 0) {
				userLevelPo = userDaoImpl.getUserLevelPoByUserId(Long.valueOf(c.getUserId())).get(0);
				userLevelPo.setLevelId(c.getLevelId());
				userLevelPo.setHigherUser(c.getHigherUser());
				userLevelPo.setProvince(c.getProvince());
				userLevelPo.setCity(c.getCity());
				userLevelPo.setCountry(c.getCountry());
				userDaoImpl.updateUserLevel(userLevelPo);

			} else {
				userLevelPo.setCompanyUser(c.getCompanyUser());
				userLevelPo.setUserId(Long.valueOf(c.getUserId()));
				userLevelPo.setLevelId(c.getLevelId());
				userLevelPo.setHigherUser(c.getHigherUser());
				userLevelPo.setProvince(c.getProvince());
				userLevelPo.setCity(c.getCity());
				userLevelPo.setCountry(c.getCountry());
				userDaoImpl.addUserLevel(userLevelPo);

			}
		} catch (Exception e) {
			throw new RuntimeException("更新失败!");
		}
		po.setUpdateTime(PubFun.getCurrentDate());
		userDaoImpl.updateUser(po);
	}

	public void updateUserForSupplier(SupplierVo c) throws GenericException {
		UserPo po = new UserPo();
		try {
			BeanUtils.copyProperties(po, c);
			// MD5对用户密码加密
			CipherUtil cipher = new CipherUtil();
			String md5Pwd = cipher.generatePassword(po.getPassword());
			po.setPassword(md5Pwd);
		} catch (Exception e) {
			throw new RuntimeException("更新失败!");
		}
		po.setUpdateTime(PubFun.getCurrentDate());
		userDaoImpl.updateUser(po);
	}

	public void deleteUser(String userId) throws GenericException {
		userDaoImpl.deleteUser(userId);
	}

	public UserVo listUserByUserId(String userId) throws GenericException {
		UserPo po = null;
		po = userDaoImpl.listUserByUserId(userId);
		UserVo vo = new UserVo();
		try {
			BeanUtils.copyProperties(vo, po);
		} catch (Exception e) {
			throw new RuntimeException("查询失败!");
		}
		// 插入供货商id
		List<SupplierPo> supplierPoList = supplierDao.getSupPoByUserId((int) po.getUserId());
		if (supplierPoList != null && supplierPoList.size() > 0) {
			vo.setSupId(supplierPoList.get(0).getSupplierId());
		}
		// 插入权限表
		List<UserLevelPo> userLeelPoList = userDaoImpl.getUserLevelPoByUserId(Long.valueOf(userId));
		if (userLeelPoList != null && userLeelPoList.size() > 0) {
			UserLevelPo userLeelPo = userLeelPoList.get(0);
			vo.setUlId(userLeelPo.getUlId());
			vo.setLevelId(userLeelPo.getLevelId());
			vo.setHigherUser(userLeelPo.getHigherUser());
			UserPo highuser = userDaoImpl.listUserByUserId(String.valueOf(userLeelPo.getHigherUser()));
			if (highuser != null) {
				vo.setHigherUserName(highuser.getRealName());
			} else {
				vo.setHigherUserName("无");
			}
			vo.setProvince(userLeelPo.getProvince());
			vo.setCity(userLeelPo.getCity());
			vo.setCountry(userLeelPo.getCountry());
			vo.setCompanyUser(userLeelPo.getCompanyUser());
		}

		return vo;
	}

	public List<RoleVo> listRoleByUserId(String userId) throws GenericException {
		List<RolePo> poList = roleDaoImpl.listAllRole();
		List<UserRolePo> userRoleList = userDaoImpl.listUserRoleByUserId(userId);
		List<RoleVo> checkedRoleList = new ArrayList<RoleVo>(); // 分配给该用户的角色
		List<RoleVo> uncheckedRoleList = new ArrayList<RoleVo>(); // 没有分配给该用户的角色
		try {
			if (poList != null && poList.size() > 1) {
				for (RolePo po : poList) {
					RoleVo vo = new RoleVo();
					BeanUtils.copyProperties(vo, po);
					int i = 0;
					for (i = 0; i < userRoleList.size(); i++) {
						UserRolePo userRolePo = userRoleList.get(i);
						if (vo.getRoleId() == userRolePo.getRoleId()) {
							vo.setChecked("true");
							break;
						}
					}
					if (i == userRoleList.size()) {
						uncheckedRoleList.add(vo);
					} else {
						checkedRoleList.add(vo);
					}
				}
			}
			checkedRoleList.addAll(uncheckedRoleList);
		} catch (Exception e) {
			throw new RuntimeException("查询失败!");
		}

		return checkedRoleList;
	}

	public void updateRoleByUserId(String userId, String[] roles) throws GenericException {
		List<UserRolePo> userRoleList = userDaoImpl.listUserRoleByUserId(userId);
		List<UserRolePo> shouldRemoveList = userRoleList;
		List<UserRolePo> shouldUpdateList = new ArrayList<UserRolePo>();
		List<UserRolePo> shouldAddList = new ArrayList<UserRolePo>();
		if (roles != null) {
			for (String roleId : roles) {
				UserRolePo userRolePo = new UserRolePo();
				userRolePo.setRoleId(Long.parseLong(roleId.trim()));
				int i = 0;
				for (i = 0; i < userRoleList.size(); i++) {
					UserRolePo oldUserRolePo = userRoleList.get(i);
					if (userRolePo.getRoleId() == oldUserRolePo.getRoleId()) {
						userRolePo = oldUserRolePo;
						shouldRemoveList.remove(i);
						break;
					}
				}
				userRolePo.setUserId(Long.parseLong(userId));
				if (i == userRoleList.size()) { // 新用户角色
					userRolePo.setInsertTime(new Date());
					shouldAddList.add(userRolePo);
				} else {
					userRolePo.setUpdateTime(new Date());
					shouldUpdateList.add(userRolePo);
				}

			}
		}

		for (int i = 0; i < shouldRemoveList.size(); i++) {
			UserRolePo userRolePo = shouldRemoveList.get(i);
			userDaoImpl.deleteUserRole(String.valueOf(userRolePo.getUserRoleId()));
		}

		for (int i = 0; i < shouldUpdateList.size(); i++) {
			UserRolePo userRolePo = shouldUpdateList.get(i);
			userDaoImpl.updateUserRole(userRolePo);
		}

		for (int i = 0; i < shouldAddList.size(); i++) {
			UserRolePo userRolePo = shouldAddList.get(i);
			userDaoImpl.addUserRole(userRolePo);
		}
	}

	public void deleteUserRoleByUserId(String userId) throws GenericException {
		userDaoImpl.deleteUserRoleByUserId(userId);
	}

	public UserPo findUserLogon(String userName, String password) throws GenericException {
		return userDaoImpl.findUserLogon(userName, password);
	}

	public List<UserPo> getUserForSup(long supId) throws GenericException {
		return userDaoImpl.getUserForSup(supId);
	}

	public List<MenuPo> getUserMenuList(String userId) throws GenericException {
		java.util.List<MenuPo> pvList = userDaoImpl.getUserMenuList(userId);
		// rootList 存放parentId=0的菜单项，也就是没有父节点
		java.util.List<MenuPo> rootList = new java.util.ArrayList<MenuPo>();
		// allPvMap 存放所有的菜单项
		java.util.Map<String, MenuPo> allPvMap = new java.util.HashMap<String, MenuPo>();
		// restPvMap存放除了parentId=0的菜单项,LinkedHashMap保证顺序
		java.util.Map<String, MenuPo> restPvMap = new java.util.LinkedHashMap<String, MenuPo>();
		for (MenuPo view : pvList) {
			String parentId = view.getParentMenuId() + "";
			if (parentId.equals("0")) {
				rootList.add(view);
			} else {
				restPvMap.put(view.getMenuId() + "", view);
			}
			allPvMap.put(view.getMenuId() + "", view);
		}

		// 循环restPvMap，根据其父节点Id(parentId)从allPvMap找,如果存在则添加为父节点的子节点
		java.util.Iterator it = restPvMap.values().iterator();
		while (it.hasNext()) {
			MenuPo view = (MenuPo) it.next();
			String parentId = view.getParentMenuId() + "";
			if (allPvMap.containsKey(parentId)) {
				allPvMap.get(parentId).getSubPurview().add(view);
			}
		}
		return rootList;
	}

	public List<RoleVo> listAllRole() throws GenericException {
		List<RolePo> poList = roleDaoImpl.listAllRole();
		List<RoleVo> voList = new ArrayList<RoleVo>();
		try {
			for (int i = 0; i < poList.size(); i++) {
				RolePo po = poList.get(i);
				RoleVo vo = new RoleVo();
				BeanUtils.copyProperties(vo, po);
				voList.add(vo);
			}
		} catch (Exception e) {
			throw new RuntimeException("查询失败!");
		}

		return voList;
	}

	public List<RoleVo> listRoleByIdentity(String identity) throws GenericException {
		List<RolePo> poList = roleDaoImpl.listRoleByIdentity(identity);
		List<RoleVo> voList = new ArrayList<RoleVo>();
		try {
			for (int i = 0; i < poList.size(); i++) {
				RolePo po = poList.get(i);
				RoleVo vo = new RoleVo();
				BeanUtils.copyProperties(vo, po);
				voList.add(vo);
			}
		} catch (Exception e) {
			throw new RuntimeException("查询失败!");
		}

		return voList;
	}

	public UserVo getUserVoById(String userId) throws GenericException {
		UserPo userPo = roleDaoImpl.findUserById(userId);
		UserVo userVo = new UserVo();

		BeanCopier copier = BeanCopier.create(UserPo.class, UserVo.class, false);
		copier.copy(userPo, userVo, null);
		userVo.setUserId(String.valueOf(userPo.getUserId()));
		return userVo;
	}

	public String getUserTestValue() throws GenericException {
		List<UserPo> userList = userDaoImpl.getAllCustUserPo();
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (userList != null && userList.size() > 0) {

			jsonUserTestValue.append("[");
			for (UserPo userPo : userList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(userPo.getUserId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(userPo.getRealName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getUserForShop() throws GenericException {
		List<UserPo> userList = userDaoImpl.getUserForShop();
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (userList != null && userList.size() > 0) {

			jsonUserTestValue.append("[");
			for (UserPo userPo : userList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(userPo.getUserId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(userPo.getRealName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String userForSupplierShop(long supId) throws GenericException {
		List<UserPo> userList = userDaoImpl.userForSupplierShop(supId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (userList != null && userList.size() > 0) {

			jsonUserTestValue.append("[");
			for (UserPo userPo : userList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(userPo.getUserId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(userPo.getRealName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getShopForSupplier(int supId) throws GenericException {
		List<ShopPo> shopList = userDaoImpl.getShopForSupplier(supId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");
			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getSupType(long supId) throws GenericException {
		List<Object> supplierTypePoList = userDaoImpl.getSupType(supId);
		String supplierType = "";
		if (supplierTypePoList != null && supplierTypePoList.size() > 0) {
			if (supplierTypePoList.get(0) != null) {
				supplierType = supplierTypePoList.get(0).toString();

			}
		}
		return supplierType;
	}

	public String getShopForAdmin(long userId) throws GenericException {
		UserLevelPo u = userDaoImpl.getUserLevelPoByUserId(userId).get(0);

		List<ShopPo> shopList = userDaoImpl.getShopForAdmin(u.getCompanyUser());
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");
			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getShopForManager(long userId) throws GenericException {
		List<ShopPo> shopList = userDaoImpl.getShopForManager(userId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");
			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String shopForAccountant(long userId) throws GenericException {
		List<ShopPo> shopList = userDaoImpl.shopForAccountant(userId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");
			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getUserTestValueForSuppOrder(String orderDate, int supId) throws GenericException {
		List<ShopPo> shopList = userDaoImpl.getUserPoByOrderDateAndSupId(orderDate, supId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");
			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	// 分货时客户列表
	public String getUserTestValueForSuppOrderDistribute(String orderDate, int supId) throws GenericException {
		List<ShopPo> shopList = userDaoImpl.getShopPoByOrderDateAndSupIdForDistribute(orderDate, supId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");
			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getUserLevel(UserVo userVo) throws GenericException {
		List<UserPo> userList = userDaoImpl.getAllUserPoBylevelOrProvinceAndCityAndCountry(userVo);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (userList != null && userList.size() > 0) {

			jsonUserTestValue.append("[");
			for (UserPo userPo : userList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(userPo.getUserId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(userPo.getRealName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getUserForSupplierCompany(SupplierVo c) throws GenericException {

		List<UserPo> userList = userDaoImpl.getUserForSupplierCompany(c);

		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (userList != null && userList.size() > 0) {

			jsonUserTestValue.append("[");
			for (UserPo userPo : userList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(userPo.getUserId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(userPo.getRealName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getCheckCust(UserVo userVo) throws GenericException {
		List<UserPo> userList = userDaoImpl.getAllUserPoBylevelOrProvinceAndCityAndCountryForcheck(userVo);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (userList != null && userList.size() > 0) {

			jsonUserTestValue.append("[");
			for (UserPo userPo : userList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(userPo.getUserId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(userPo.getRealName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getPullDownProvince(String space) throws GenericException {
		List<LdAddressPo> provinceList = userDaoImpl.getProvince();
		StringBuffer jsonProvicePull = new StringBuffer("");

		if (provinceList != null && provinceList.size() > 0) {

			jsonProvicePull.append("[");
			if ("1".equals(space)) {
				jsonProvicePull.append("{\"");
				jsonProvicePull.append("areaid");
				jsonProvicePull.append("\":\"");
				jsonProvicePull.append("0");
				jsonProvicePull.append("\",\"name\":\"");
				jsonProvicePull.append("全部");
				jsonProvicePull.append("\"},");
			}

			for (LdAddressPo ldAddressPo : provinceList) {
				jsonProvicePull.append("{\"");
				jsonProvicePull.append("areaid");
				jsonProvicePull.append("\":\"");
				jsonProvicePull.append(ldAddressPo.getPlacecode());
				jsonProvicePull.append("\",\"name\":\"");
				jsonProvicePull.append(ldAddressPo.getPlacename());
				jsonProvicePull.append("\"},");
			}
			jsonProvicePull = new StringBuffer(jsonProvicePull.substring(0, jsonProvicePull.length() - 1));
			jsonProvicePull.append("]");
		}
		return jsonProvicePull.toString();
	}

	public String getPullDownCity(int parentId, String space) throws GenericException {
		List<LdAddressPo> cityList = userDaoImpl.getCity(parentId);
		StringBuffer jsonCityPull = new StringBuffer("");

		if (cityList != null && cityList.size() > 0) {

			jsonCityPull.append("[");
			if ("1".equals(space)) {
				jsonCityPull.append("{\"");
				jsonCityPull.append("areaid");
				jsonCityPull.append("\":\"");
				jsonCityPull.append("0");
				jsonCityPull.append("\",\"name\":\"");
				jsonCityPull.append("全部");
				jsonCityPull.append("\"},");
			}
			for (LdAddressPo ldAddressPo : cityList) {
				jsonCityPull.append("{\"");
				jsonCityPull.append("areaid");
				jsonCityPull.append("\":\"");
				jsonCityPull.append(ldAddressPo.getPlacecode());
				jsonCityPull.append("\",\"name\":\"");
				jsonCityPull.append(ldAddressPo.getPlacename());
				jsonCityPull.append("\"},");
			}
			jsonCityPull = new StringBuffer(jsonCityPull.substring(0, jsonCityPull.length() - 1));
			jsonCityPull.append("]");
		}
		return jsonCityPull.toString();
	}

	public String getPullDownCountry(int parentId, String space) throws GenericException {
		List<LdAddressPo> countryList = userDaoImpl.getCountry(parentId);
		StringBuffer jsonCountryPull = new StringBuffer("");

		if (countryList != null && countryList.size() > 0) {

			jsonCountryPull.append("[");
			if ("1".equals(space)) {
				jsonCountryPull.append("{\"");
				jsonCountryPull.append("areaid");
				jsonCountryPull.append("\":\"");
				jsonCountryPull.append("0");
				jsonCountryPull.append("\",\"name\":\"");
				jsonCountryPull.append("全部");
				jsonCountryPull.append("\"},");
			}
			for (LdAddressPo ldAddressPo : countryList) {
				jsonCountryPull.append("{\"");
				jsonCountryPull.append("areaid");
				jsonCountryPull.append("\":\"");
				jsonCountryPull.append(ldAddressPo.getPlacecode());
				jsonCountryPull.append("\",\"name\":\"");
				jsonCountryPull.append(ldAddressPo.getPlacename());
				jsonCountryPull.append("\"},");
			}
			jsonCountryPull = new StringBuffer(jsonCountryPull.substring(0, jsonCountryPull.length() - 1));
			jsonCountryPull.append("]");
		}
		return jsonCountryPull.toString();
	}

	public UserLevelPo getUserLevelPo(long userId) throws GenericException {
		return userDaoImpl.getUserLevelPoByUserId(userId).get(0);
	}

	public List<ShopPo> getUsePoListByUserIdString(String userIdString) throws GenericException {
		return userDaoImpl.getUserPoListByUserIdString(userIdString);

	}

	public List<UserPo> getUsePoListByOpenId(String openId) throws GenericException {
		return userDaoImpl.getUserPoListByOpenId(openId);

	}

	public boolean getUserNameForCheck(String userName) throws GenericException {
		List<UserPo> list = userDaoImpl.getUserPoListByUserName(userName);
		if (list != null && list.size() > 0) {
			return false;
		}
		return true;
	}

	public boolean getRealNameForCheck(String userName) throws GenericException {
		List<UserPo> list = userDaoImpl.getUserPoListByRealName(userName);
		if (list != null && list.size() > 0) {
			return false;
		}
		return true;
	}

	public boolean getCheckPhone(String phone) throws GenericException {
		List<UserPo> list = userDaoImpl.getUserPoListByUserPhone(phone);
		if (list != null && list.size() > 0) {
			return false;
		}
		return true;
	}
}
